﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HotelResSystem2
{
    public partial class FormBill : Form
    {
        double currentBalance = 0.0;

        public FormBill(Bill bill)
        {
            InitializeComponent();
            currentBalance = bill.GrandTotal;
            textBoxTotalDue.Text = "$" + currentBalance;
            CalculateBalance();
            textBoxAmountPayed.Focus();
        }

        private void CalculateBalance()
        {
            double payment = 0.0;
            if (double.TryParse(textBoxAmountPayed.Text, out payment))
            {
                textBoxBalance.Text = payment >= currentBalance ? "-" : "";
                textBoxBalance.Text += Math.Abs(currentBalance - payment);
            }
        }


        private void textBoxAmountPayed_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow digits and decimal points
            e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back);
        }

        private void buttonPay_Click(object sender, EventArgs e)
        {
            double amountPayed = 0.0;
            CalculateBalance();
            if (!double.TryParse(textBoxAmountPayed.Text, out amountPayed))
            {
                MessageBox.Show("Please enter a payment before continuing.", "No Payment", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxAmountPayed.Focus();
                return;
            }
            currentBalance = double.Parse(textBoxBalance.Text);
            if (currentBalance > 0.0)
            {
                MessageBox.Show("Another payment is needed to pay this bill.", "Further Payment Necessary", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxTotalDue.Text = currentBalance.ToString();
                textBoxAmountPayed.Text = "";
                textBoxBalance.Text = "";
            }
            else
            {
                MessageBox.Show("Required payment has been processed.", "Payment Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
